package com.controller;

import cn.hutool.json.JSONUtil;
import com.common.ControllerSupport;
import com.basemodel.Result;
import com.exception.ApiException;
import com.model.DataInfo;
import com.model.TableColumn;
import com.service.GenerateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author: cxt
 * @time: 2022/6/20
 */
@Controller
public class GenerateController extends ControllerSupport {

    private GenerateService generateService;

    @GetMapping({"/index", "/"})
    public String index() {
        return "/add";
    }

    /**
     * 添加数据信息
     */
    @PostMapping("/add/datasource")
    @ResponseBody
    public Result<String> add(@RequestBody @Valid DataInfo dataBase, BindingResult bindingResult) {
        validErrorHandler(bindingResult);
        generateService.initData(dataBase);
        return Result.success("添加成功");
    }

    /**
     * 删除列表元素
     */
    @GetMapping("/remove")
    @ResponseBody
    public Result<String> remove(@RequestParam String dbName) {
        generateService.remove(dbName);
        return Result.success("删除成功");
    }

    /**
     * 数据源页面
     */
    @GetMapping("/get/source")
    public String dataSource(Model model) {
        List<DataInfo> list = generateService.getSources();
        String s = JSONUtil.toJsonStr(list);
        model.addAttribute("list", s);
        return "/source";
    }

    /**
     * 连接数据,返回数据库表
     */
    @GetMapping("/connect/db")
    @ResponseBody
    public Result<String> connect(@RequestParam String dbName) {
        generateService.connect(dbName);
        return Result.success("连接成功");
    }

    /**
     * 数据库具体表
     */
    @GetMapping("/get/table")
    public String getTable(@RequestParam("dbName") String dbName, Model model) {
        String url = "/tableInfo";
        try {
            String dbInfo = generateService.getTable(dbName);
            model.addAttribute("dbInfo", dbInfo);
            model.addAttribute("dbName", dbName);
        } catch (ApiException e) {
            url = "/source";
            e.printStackTrace();
        }
        return url;
    }

    /**
     * 查询表信息
     *
     * @param dbName    数据名
     * @param tableName 表名
     * @return
     */
    @GetMapping("/query/table/column")
    @ResponseBody
    public List<TableColumn> getTableColumn(@RequestParam("dbName") String dbName, @RequestParam("tableName") String tableName) {
        return generateService.getTableColumn(dbName, tableName);
    }

    /**
     * 新建表
     *
     * @param sql 执行的SQL
     * @return 新建后的表列表
     */
    @PostMapping("/create/sentence")
    @ResponseBody
    public List<String> createTableByStr(@RequestParam("sql") String sql, @RequestParam("dbName") String dbName) {
        return generateService.createTableByStr(sql, dbName);
    }

    @Autowired
    public void setGenerateService(GenerateService generateService) {
        this.generateService = generateService;
    }
}
